// 手写promise.all
const Promiseall = (array) => {
  // 对传进来的类数组 数组化
  let promises = Array.from(array);
  let length = promises.length;
  let count = 0;
  //   存储每个promise对应的数据
  let data = [];
  return new Promise((resolve, reject) => {
    for (let i in promises) {
      promises[i]
        .then((res) => {
          data[i] = res;
          //   全部存储完 返回数据
          if (++count == length) {
            resolve(data);
          }
        })
        // 只要有一个reject 直接返回
        .catch((err) => {
          reject(err);
        });
    }
  });
};

let promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("promise1");
  }, 1000);
});
let promise2 = Promise.resolve("promise2");
let promise3 = new Promise((resolve, reject) => {
  resolve("promise3");
});

Promiseall([promise1, promise2, promise3]).then(
  (res) => {
    console.log(res);
  },
  (err) => {
    console.log(err);
  }
);
